/**   
 * @Title: BallMenuBuilder.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: (创建通用选球菜单) 
 * @author City Jiangsy@500wan.com   
 * @date 2011-6-17 下午07:27:03  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.esun.config.Config;
import com.esun.model.DaletouInfo;
import com.esun.model.ShuangseqiuInfo;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

/**
 * @ClassName: BallMenuBuilder
 * @Description: 创建通用选球菜单
 * @author City Jiangsy@500wan.com
 * @date 2011-6-17 下午07:27:03
 * 
 */
public class BallMenuBuilder {
	private Context context;
	private BallItemAdpter ballItemAdpter;
	private List<Map<String, Object>> listBallItem; // 单个球状态
	private int keyboadScale; // 菜单规模,int
	private int ballItemId; // 小球xml的id
	private int ballItemLayoutId; // 小球xml layout的id
	private GridView gridKeyboard; // 承载菜单的gridview
	private int colorCode = 0; // 小球颜色代码,int,1为蓝球,其他全为红球,红球缺省为0
	private boolean isCheckable = true;
	private int checkedCount = 0; // 计数器,记录选中的小球数
	private int limitCount; // 选球上限
	private TextView tvBetNum; // 显示注数
	private TextView tvMoney; // 显示金额
	private String lotId; // 彩种代码

	private List<String> listResult;

	/**
	 * <p>
	 * Title: BallMenuBuilder
	 * </p>
	 * <p>
	 * Description: 构造函数
	 * </p>
	 * 
	 * @param context
	 *            调用该类的activity
	 * @param keyboadScale
	 *            键盘规模,int
	 * @param colorCode
	 *            颜色代码,int,1为蓝球,其他全为红球,红球缺省为0
	 * @param limitCount
	 *            选球上限,int
	 */
	public BallMenuBuilder(Context context, int keyboadScale, int limitCount, int colorCode, TextView tvBetNum, TextView tvMoney, String lotId) {
		super();
		this.context = context;
		this.keyboadScale = keyboadScale;
		this.colorCode = colorCode;
		this.limitCount = limitCount;
		this.tvBetNum = tvBetNum;
		this.tvMoney = tvMoney;
		this.lotId = lotId;
		initBallMenuBuilder();
	}

	/**
	 * @Title: initBallMenuBuilder
	 * @Description: (初始化构造函数)
	 * @param 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	private void initBallMenuBuilder() {
		
		if (colorCode == 1) {
			ballItemId = R.id.ballmenuitem_cbx_ballBlue;
			ballItemLayoutId = R.layout.ball_menu_item_blue;
		} else {
			ballItemId = R.id.ballmenuitem_cbx_ballRed;
			ballItemLayoutId = R.layout.ball_menu_item_red;
		}
		// 初始化GridView
		gridKeyboard = (GridView) ((Activity) context).getLayoutInflater().inflate(R.layout.ball_menu_keyboad, null);
		// 初始化list
		Map<String, Object> map = new HashMap<String, Object>();
		listBallItem = new ArrayList<Map<String, Object>>();
		map.put("cbx", false);
		for (int i = 0; i < keyboadScale; i++) {
			listBallItem.add(map);
		}

		tvBetNum.setText(Config.ONESPACE + Config.NUM0 + Config.ONESPACE);
		tvMoney.setText(Config.ONESPACE + Config.NUM0 + Config.ONESPACE);

		listResult = new ArrayList<String>();

		cleanInfo();
	}

	/**
	 * @Title: setKeyBoad
	 * @Description: (显示选球菜单)
	 * @param @return gridKeyboard
	 * @return View 返回类型
	 * @throws
	 */
	public View setKeyBoad() {
		listResult.clear();
		ballItemAdpter = new BallItemAdpter(context, ballItemId, "cbx", ballItemLayoutId, isCheckable);
		gridKeyboard.setAdapter(ballItemAdpter);
		return gridKeyboard;
	}

	public View cleanKeyBoad() {
		checkedCount = 0;
		isCheckable = true;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cbx", false);
		for (int i = 0; i < keyboadScale; i++) {
			listBallItem.set(i, map);
		}
		listResult.clear();
		ballItemAdpter = new BallItemAdpter(context, ballItemId, "cbx", ballItemLayoutId, isCheckable);
		gridKeyboard.setAdapter(ballItemAdpter);

		tvBetNum.setText(Config.ONESPACE + Config.NUM0 + Config.ONESPACE);
		tvMoney.setText(Config.ONESPACE + Config.NUM0 + Config.ONESPACE);

		cleanInfo();

		return gridKeyboard;
	}

	/**
	 * @Title: setKeyBoadRandom
	 * @Description: (返回一组有特定选中状态小球的菜单)
	 * @param @param randomNum
	 * @param @return gridKeyboard
	 * @return View 返回类型
	 * @throws
	 */
	public View setKeyBoadRandom(ArrayList<Integer> randomNum) {
		isCheckable = true;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cbx", false);
		for (int i = 0; i < keyboadScale; i++) {
			listBallItem.set(i, map);
		}
		Map<String, Object> randomMap = new HashMap<String, Object>();
		randomMap.put("cbx", true);

		listResult.clear();
		for (int i = 0; i < randomNum.size(); i++) {
			listBallItem.set(randomNum.get(i) - 1, randomMap);
			listResult.add(Integer.toString(randomNum.get(i)));
		}
		checkedCount = randomNum.size();
		ballItemAdpter = new BallItemAdpter(context, ballItemId, "cbx", ballItemLayoutId, isCheckable);
		gridKeyboard.setAdapter(ballItemAdpter);

		refreshInfo();
		return gridKeyboard;
	}

	/**
	 * @Title: setKeyboardDisable
	 * @Description: (将键盘clickable设为false)
	 * @param @return gridKeyboard
	 * @return View 返回类型
	 * @throws
	 */
	public View setKeyboardDisable() {
		isCheckable = false;
		ballItemAdpter = new BallItemAdpter(context, ballItemId, "cbx", ballItemLayoutId, isCheckable);
		gridKeyboard.setAdapter(ballItemAdpter);
		return gridKeyboard;
	}

	/**
	 * @Title: setKeyboardEnable
	 * @Description: (将键盘clickable设为true)
	 * @param @return gridKeyboard
	 * @return View 返回类型
	 * @throws
	 */
	public View setKeyboardEnable() {
		isCheckable = true;
		ballItemAdpter = new BallItemAdpter(context, ballItemId, "cbx", ballItemLayoutId, isCheckable);
		gridKeyboard.setAdapter(ballItemAdpter);
		return gridKeyboard;
	}

	/**
	 * @ClassName: BallItemAdpter
	 * @Description: (构建单个小球的adapter)
	 * @author City Jiangsy@500wan.com
	 * @date 2011-6-20 下午01:29:36
	 * 
	 */
	class BallItemAdpter extends BaseAdapter {

		private Context context;
		private int layoutId;
		private int itemId;
		private String itemName;
		private boolean isCheckable;

		public BallItemAdpter(Context context, int itemId, String itemName, int layoutId, boolean isCheckable) {
			super();
			this.context = context;
			this.itemId = itemId;
			this.itemName = itemName;
			this.layoutId = layoutId;
			this.isCheckable = isCheckable;
		}

		@Override
		public int getCount() {
			
			return listBallItem.size();
		}

		@Override
		public Object getItem(int position) {
			
			return 0;
		}

		@Override
		public long getItemId(int position) {
			
			return position;
		}

		// private int count;
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
		
			convertView = LayoutInflater.from(context).inflate(layoutId, null);
			CheckBox cbx = (CheckBox) convertView.findViewById(itemId);
			cbx.setText(Integer.toString(position + 1));

			// 从list中去除小球状态并显示
			cbx.setChecked((Boolean) listBallItem.get(position).get(itemName));

			// 标志位,是否显示提示信息
//			final boolean isShowTip = cbx.isChecked();

			// 同步checkbox可用状态
			if (cbx.isChecked()) {
				cbx.setClickable(true);
			} else {
				cbx.setClickable(isCheckable);
			}
			cbx.setOnCheckedChangeListener(new OnCheckedChangeListener() {		
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					Map<String, Object> checkMap = new HashMap<String, Object>();
					checkMap.put("cbx", isChecked);
					listBallItem.set(position, checkMap);
					if (isChecked) {
						checkedCount++;
						if (checkedCount > limitCount) {
							checkedCount --;
							buttonView.setChecked(false);
							Toast.makeText(context, "最多选择" + limitCount + "个", Toast.LENGTH_SHORT).show();
						}else if (isOverMoney()){
							checkedCount --;
							buttonView.setChecked(false);
							Toast.makeText(context, strMsg(), Toast.LENGTH_SHORT).show();		
							
						}else {
							
							listResult.add(Integer.toString(position + 1));
						}
						
						refreshInfo();
					} else {
						checkedCount--;
						if ((checkedCount < limitCount) && !isCheckable) {
							setKeyboardEnable();
						}
						listResult.remove(Integer.toString(position + 1));
						refreshInfo();
					}
					EDebug.o(BallMenuBuilder.this,"checkedCount----------------"+checkedCount);
					for(int i = 0 ;i < listResult.size(); i++) {
						EDebug.o(BallMenuBuilder.this,"已选中--------------"+listResult.get(i));
					}
				}
			});
			return convertView;
		}
	}

	public List<String> getListResult() {

		TreeSet<Integer> tree = new TreeSet<Integer>();
		ArrayList<String> alRet = new ArrayList<String>();
		for (int i = 0; i < listResult.size(); i++) {
			int intTmp = Integer.parseInt(listResult.get(i));
			tree.add(intTmp);
		}

		Iterator<Integer> iter = tree.iterator();
		while (iter.hasNext()) {
			alRet.add(String.valueOf(iter.next()));
		}
		return alRet;
	}

	// 还你妹能让我更尴尬点么...
	public void refreshInfo() {
		if (lotId.equals(Config.SHUANGSEQIU)) {
			if (colorCode == 1) {
				ShuangseqiuInfo.getShuangseqiuInfo().setBlueBallCount(getListResult().size());
				tvBetNum.setText(Config.ONESPACE + Integer.toString(ShuangseqiuInfo.getShuangseqiuInfo().getBetCount()) + Config.ONESPACE);
				tvMoney.setText(Config.ONESPACE + Integer.toString(2 * ShuangseqiuInfo.getShuangseqiuInfo().getBetCount()) + Config.ONESPACE);
				EDebug.o(BallMenuBuilder.this,"money--------"+ Integer.toString(2 * ShuangseqiuInfo.getShuangseqiuInfo().getBetCount()));
			} else {
				ShuangseqiuInfo.getShuangseqiuInfo().setRedBallCount(getListResult().size());
				tvBetNum.setText(Config.ONESPACE + Integer.toString(ShuangseqiuInfo.getShuangseqiuInfo().getBetCount()) + Config.ONESPACE);
				tvMoney.setText(Config.ONESPACE + Integer.toString(2 * ShuangseqiuInfo.getShuangseqiuInfo().getBetCount()) + Config.ONESPACE);
				EDebug.o(BallMenuBuilder.this,"money--------"+ Integer.toString(2 * ShuangseqiuInfo.getShuangseqiuInfo().getBetCount()));
			}
		} else if (lotId.equals(Config.DALETOU)) {
			if (colorCode == 1) {
				DaletouInfo.getDaletouInfo().setBlueBallCount(getListResult().size());
				tvBetNum.setText(Config.ONESPACE + Integer.toString(DaletouInfo.getDaletouInfo().getBetCount()) + Config.ONESPACE);
				tvMoney.setText(Config.ONESPACE + Integer.toString(2 * DaletouInfo.getDaletouInfo().getBetCount()) + Config.ONESPACE);
			} else {
				DaletouInfo.getDaletouInfo().setRedBallCount(getListResult().size());
				tvBetNum.setText(Config.ONESPACE + Integer.toString(DaletouInfo.getDaletouInfo().getBetCount()) + Config.ONESPACE);
				tvMoney.setText(Config.ONESPACE + Integer.toString(2 * DaletouInfo.getDaletouInfo().getBetCount()) + Config.ONESPACE);
			}
		}
	}

	// 欲哭无泪 啊
	private void cleanInfo() {
		if (lotId.equals(Config.SHUANGSEQIU)) {
			if (colorCode == 1) {
				ShuangseqiuInfo.getShuangseqiuInfo().setBlueBallCount(0);
			} else {
				ShuangseqiuInfo.getShuangseqiuInfo().setRedBallCount(0);
			}
		} else if (lotId.equals(Config.DALETOU)) {
			if (colorCode == 1) {
				DaletouInfo.getDaletouInfo().setBlueBallCount(0);
			} else {
				DaletouInfo.getDaletouInfo().setRedBallCount(0);
			}
		}
	}
	private boolean isOverMoney () {
		boolean isOver = false;
		int money ;
		if (lotId.equals(Config.SHUANGSEQIU)) {
			int RedBallNum = ShuangseqiuInfo.getShuangseqiuInfo().getRedBallCount();
			int BlueBallNum = ShuangseqiuInfo.getShuangseqiuInfo().getBlueBallCount();
			if (colorCode == 1) {
				money = 2*RedBallNum*(RedBallNum-1)*(RedBallNum-2)*(RedBallNum-3)*(RedBallNum-4)*(RedBallNum-5)*(BlueBallNum+1)/(6*5*4*3*2*1);		
			}else {
				money = 2*(RedBallNum+1)*RedBallNum*(RedBallNum-1)*(RedBallNum-2)*(RedBallNum-3)*(RedBallNum-4)*(BlueBallNum)/(6*5*4*3*2*1);
			}
			if(money > Config.MAXIMUM_BET_SHUANGSEQIU){
				isOver = true;
			}
		}else if (lotId.equals(Config.DALETOU)) {
			int RedBallNum = DaletouInfo.getDaletouInfo().getRedBallCount();
			int BlueBallNum = DaletouInfo.getDaletouInfo().getBlueBallCount();
			if(colorCode == 1) {
				money = 2*DaletouInfo.getVoteline(RedBallNum, 5)*DaletouInfo.getVoteline(BlueBallNum + 1, 2);
			}else {
				money = 2*DaletouInfo.getVoteline(RedBallNum + 1, 5)*DaletouInfo.getVoteline(BlueBallNum, 2);
			}
			if(money > Config.MAXIMUM_BET_DALETOU){
				isOver = true;
			}
		}
		return isOver;
	}
	private String strMsg () {
		String msg = null;
		if (lotId.equals(Config.SHUANGSEQIU)) {
			msg = "一注最高10万元";
		}else if (lotId.equals(Config.DALETOU)) {
			msg = "一注最高2万元";
		}
		return msg;
	}
}
